package com.yanqu.road.server.manger.activity.fund;

import com.yanqu.road.dao.impl.activity.fund.UserActivityFundDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.fund.UserActivityFundData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.math.BigInteger;
import java.util.List;

public class ActivityFundMgr extends TempMgr {

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 根据战令进度。玩家不存在则更新数据库
     * @param activityId
     * @param userId
     * @param progress
     */
    public static void updateProgress(int activityId, long userId, long progress) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {
            //战令积分变更
            player.getModule(ActivityFundModule.class).updateProgress(activityId, progress);
        } else {
            //锁一下玩家
            synchronized (GamePlayerMgr.getPlayerLock(userId)) {
                //玩家不在线，更新数据库
                UserActivityFundDataDaoImpl activityFundDataDao = new UserActivityFundDataDaoImpl();
                //更新UserActivityFundData
                UserActivityFundData userActivityFundData = activityFundDataDao.getUserActivityFundData(activityId, userId);
                boolean updateConditionFlag = false;
                if (userActivityFundData != null) {
                    if (progress > userActivityFundData.getProgress()) {
                        userActivityFundData.setProgress(progress);
                        activityFundDataDao.update(userActivityFundData);
                        updateConditionFlag = true;
                    }
                } else {
                    userActivityFundData = new UserActivityFundData();
                    userActivityFundData.setActivityId(activityId);
                    userActivityFundData.setUserId(userId);
                    userActivityFundData.setProgress(progress);
                    activityFundDataDao.add(userActivityFundData);
                    updateConditionFlag = true;
                }
                if (!updateConditionFlag) {
                    return;
                }
                //更新condition
                long now = System.currentTimeMillis() / 1000;
                List<UserActivityConditionData> tmpConditionDataList = UserActivityBussiness.getUserActivityConditionDataListByUserIdAndType(activityId, userId, eGamePlayerEventType.CommonActivityFundProgress.getValue());
                for (UserActivityConditionData tmpConditionData : tmpConditionDataList) {
                    //获取condition配置
                    ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, tmpConditionData.getConditionId());
                    if (conditionInfo == null) {
                        continue;
                    }
                    //已经完成的condition，不更新
                    if (tmpConditionData.getValue().compareTo(conditionInfo.getParamList().get(3)) >= 0) {
                        continue;
                    }
                    //判断是否有需要购买礼包才能更新
                    List<BigInteger> paramList = conditionInfo.getParamList();
                    int mallKey = paramList.get(0).intValue();
                    if (mallKey != 0) {
                        // 付费
                        MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityId, mallKey);
                        if (mallInfo == null) {
                            continue;
                        }
                        int mallKeyId = Integer.parseInt(mallInfo.getExtendParam());
                        boolean isBuy = userActivityFundData.isBuyByMallKey(mallKeyId);
                        if (!isBuy) {
                            continue;
                        }
                    }
                    if (!tmpConditionData.isGetReward() && BigInteger.valueOf(progress).compareTo(tmpConditionData.getValue()) > 0) {
                        tmpConditionData.setValue(BigInteger.valueOf(progress));
                        tmpConditionData.setModifyTime(now);
                        // 更新condition
                        UserActivityBussiness.updateUserActivityConditionData(tmpConditionData);
                    }
                }
            }
        }
    }
}
